package com.itheima.controller;

import com.itheima.annotation.Log;
import com.itheima.entity.Emp;
import com.itheima.entity.EmpQueryParam;
import com.itheima.entity.PageBean;
import com.itheima.entity.Result;
import com.itheima.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Slf4j
@RequestMapping("/emps")
@RestController
public class EmpController {
    @Autowired
    private EmpService empService;
//    @GetMapping("/emps")
//    public Result page(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pageSize)
//    {
//        log.info("分页查询：{},{}",page,pageSize);
//        PageBean pageBean= empService.page(page,pageSize);
//        return Result.success(pageBean);
//    }

    @GetMapping
    public Result page(EmpQueryParam param){
        log.info("分页查询：{}, {}, {},{}, {}, {}", param.getName(), param.getGender(), param.getBegin(), param.getEnd(), param.getPage(), param.getPageSize());
        PageBean pageBean = empService.page(param);
        return Result.success(pageBean);
    }
    @Log
    @PostMapping
    public Result save(@RequestBody Emp emp) throws Exception
    {
        log.info("新增员工：{}",emp);
        empService.save(emp);
        return Result.success();
    }
    @Log
    @DeleteMapping
    public Result delete(@RequestParam List<Integer> ids)//方法1：Integer []，方法二：@RequestParam List
    {
        log.info("删除员工，ids=：{}", ids);
        empService.delete(ids);
        return Result.success();
    }
    @GetMapping("/{id}")
    public Result getId(@PathVariable Integer id)
    {
        log.info("回显员工，id=：{}", id);
        Emp emp = empService.getById(id);
        return Result.success(emp);
    }
    @Log
    @PutMapping
    public Result update(@RequestBody Emp emp)
    {
        log.info("修改员工：emp={}", emp);
        empService.update(emp);
        return Result.success();
    }
    @GetMapping("/list")
    public Result list(){
        log.info("查询所有的员工数据");
        List<Emp> empList = empService.list();
        return Result.success(empList);
    }
}
